/**
 * Copyright (c) 2011-2012
 * OmniCore CodeGuide
 */
package de.tum.in.oose.bumpers.ui;


import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import de.tum.in.oose.bumpers.cars.UserCar;

/**
 * MouseSeeting.
 */
public class MouseSteering extends MouseAdapter {
	private UserCar theDrivenCar;
	private GameBoard theField;
	
	/**
	 * @param playingField the Gameboard
	 * @param dc the drive user car
	 */
    public MouseSteering(GameBoard playingField, UserCar dc) {
		theDrivenCar = dc;
		this.theField = playingField;
		this.theField.addMouseListener(this);
    }
	
	/**
	 * @param e the mouse event
	 */
    public void mousePressed(MouseEvent e) {
		Point carPosition = theDrivenCar.getPosition();
		Point clickPosition = theField.convertPosition(new Point(e.getX(),
																 e.getY()));
		int deltaX = clickPosition.x - carPosition.x;
		deltaX = Math.abs(deltaX);
		int deltaY = clickPosition.y - carPosition.y;
		double diff = ((double)deltaY)/((double)deltaX);
		double theta = Math.atan(diff);
		int degree = (int)Math.toDegrees(theta);
		
		if(clickPosition.x>carPosition.x){
			degree = 90-degree;
		}else{
			degree = 270+degree;
		}
		theDrivenCar.setDirection(degree);
    }
}
